#!/bin/bash

#=======================================================================
# ga-other
# File ID: 8ccdcd38-f1c3-11e3-96ba-c80aa9e67bbd
#
# Read filenames from stdin and store the git-annex key to 
# /kopiert-annex/$1. For documenting where I got files from or who I've 
# given a copy to. If all copies should be destroyed (FSM forbid) I know 
# where to get another copy. Example:
#
# ls | ga-other Some_Bloke
# find -type l | ga-other Another_Bloke
#
# License: GNU General Public License version 2 or later.
#=======================================================================

progname=ga-other

test -z "$1" && { echo $progname: Name not specified >&2; exit 1; }
topdir="$(git rev-parse --show-toplevel)"
kopidir="$topdir/kopiert-annex"
test -d "$kopidir" || mkdir "$kopidir" || \
    { echo $progname: $kopidir: Cannot create directory >&2; exit 1; }
filename="$1.txt"
destfile="$kopidir/$filename"

while read f; do
    key=$(git annex lookupkey "$f")
    test -n "$key" && echo "$key" >>"$destfile"
done

sort -u "$destfile" >"$destfile.tmp"
mv "$destfile.tmp" "$destfile"
